
package com.onemore.callinfo;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.onemore.callinfo.facebookauth.Hackbook;
import com.onemore.callinfo.setting.SettingMainActivity;

public class TabHostActivity extends TabActivity {
    private TabHost mTabHost = null;

    private static final int TAB_INDEX_1 = 1;

    private static final int TAB_INDEX_2 = 2;

    private static final int TAB_INDEX_3 = 3;

    private static final String TAB_TAG_1 = "ranking";

    private static final String TAB_TAG_2 = "carelist";
    
    private static final String TAB_TAG_3 = "facebook";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.tab_main);

        mTabHost = getTabHost();
        // Intent intent1 = new Intent(this, MainActivity.class);
        // Intent intent2 = new Intent(this, Main2Activity.class);
        // Intent intent3 = new Intent(this, Hackbook.class);
        
        Button btnSetting = (Button) findViewById(R.id.setting_btn);
        btnSetting.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(TabHostActivity.this, SettingMainActivity.class);
				startActivity(intent);
			}
		});

        this.setupTab(new TextView(this), TAB_TAG_1, TAB_INDEX_1, FriendRankingActvitiy.class);
        this.setupTab(new TextView(this), TAB_TAG_2, TAB_INDEX_2, FriendCareActivity.class);
        this.setupTab(new TextView(this), TAB_TAG_3, TAB_INDEX_3, Hackbook.class);
        
        getTabWidget().getChildTabViewAt(0).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_focus);

        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                
                if(tabId.endsWith(TAB_TAG_1))
                {
                    getTabWidget().getChildTabViewAt(0).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_focus);
                    getTabWidget().getChildTabViewAt(1).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_normal);
                    getTabWidget().getChildTabViewAt(2).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_normal);
                }
                else if(tabId.endsWith(TAB_TAG_2))
                {
                    getTabWidget().getChildTabViewAt(0).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_normal);
                    getTabWidget().getChildTabViewAt(1).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_focus);
                    getTabWidget().getChildTabViewAt(2).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_normal); 
                }
                else if(tabId.endsWith(TAB_TAG_3))
                {
                    getTabWidget().getChildTabViewAt(0).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_normal);
                    getTabWidget().getChildTabViewAt(1).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_normal);
                    getTabWidget().getChildTabViewAt(2).findViewById(R.id.tab_contents).setBackgroundResource(R.drawable.sub_sns_info_box_focus);  
                }
                else
                {
                    
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

    }

    private void setupTab(final View view, final String tag, final int index, Class _class) {
        View tabview = createTabView(mTabHost.getContext(), index);
        Intent intent = new Intent(this, _class);
        TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
        
        mTabHost.addTab(setContent);

    }

    private static View createTabView(final Context context, final int index) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_bg, null);
        ImageView ivItemImage = (ImageView)view.findViewById(R.id.tab_ivItemImage);
        LinearLayout tab = (LinearLayout)view.findViewById(R.id.tab_contents);
        
        switch (index) {
            case TAB_INDEX_1:
                ivItemImage.setImageResource(R.drawable.tab_call_logo);
                break;

            case TAB_INDEX_2:
                ivItemImage.setImageResource(R.drawable.tab_friend_logo);
                break;

            case TAB_INDEX_3:
                ivItemImage.setImageResource(R.drawable.tab_facebook_logo);
                break;

            default:
                break;
        }

        return view;
    }
}
